﻿using UnityEngine;
using System.Collections;

public class Inform : MonoBehaviour {
    GameObject objDescLabel;
    Vector3 WorldScreenSize;
    Vector2 BoxSize;

    public string m_strName;
    public string m_strDesc;

    bool m_bFadeOut;

	// Use this for initialization
	void Start () {

        objDescLabel = Resources.Load("Prefaps/UI/StaticUI/Inform/InformDesc") as GameObject;

        WorldScreenSize = UICamera.mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height));
        BoxSize = new Vector2(transform.GetChild(0).GetComponent<UISprite>().width * transform.GetChild(0).localScale.x, transform.GetChild(0).GetComponent<UISprite>().height * transform.GetChild(0).localScale.y);

        transform.localScale = Vector3.one;

        int iDescLength = 1;
        for (int i = 0; i < m_strDesc.Length; ++i)
        {
            if (m_strDesc[i] == '\n')
                iDescLength += 1;
        }

        int iLineChg = 0;
        int iNextLineChg = 0;
        for (int i = 0; i < iDescLength; ++i)
        {
            GameObject TempDesc;
            TempDesc = Instantiate(objDescLabel) as GameObject;
            TempDesc.transform.parent = transform;
            TempDesc.transform.localScale = new Vector3(0.1f, 0.1f);


            TempDesc.transform.localPosition = new Vector3(125.0f, -87.5f + ((iDescLength-1) * 10) - (i * 20));

            iNextLineChg = m_strDesc.IndexOf("\n", iLineChg) + 1;

            if (iNextLineChg != 0) // 다음줄이 있을때
            {
                if (m_strDesc.IndexOf("+", iLineChg, iNextLineChg - iLineChg) != -1)
                {
                    string strTemp = m_strDesc.Substring(m_strDesc.IndexOf("+", iLineChg), iNextLineChg - m_strDesc.IndexOf("+", iLineChg ) -2);

                    TempDesc.GetComponent<UILabel>().text = m_strDesc.Substring(iLineChg, m_strDesc.IndexOf("+", iLineChg) - iLineChg - 1);
                    TempDesc.transform.FindChild("Plus").GetComponent<UILabel>().text = strTemp;
                }
                else if (m_strDesc.IndexOf("-", iLineChg, iNextLineChg - iLineChg) != -1)
                {
                    string strTemp = m_strDesc.Substring(m_strDesc.IndexOf("-", iLineChg), iNextLineChg - m_strDesc.IndexOf("-", iLineChg) - 2);

                    TempDesc.GetComponent<UILabel>().text = m_strDesc.Substring(iLineChg, m_strDesc.IndexOf("-", iLineChg) - iLineChg - 1);
                    TempDesc.transform.FindChild("Minus").GetComponent<UILabel>().text = strTemp;
                }
                else
                    TempDesc.GetComponent<UILabel>().text = m_strDesc.Substring(iLineChg, iNextLineChg - iLineChg - 1);
            }
            else // 마지막 줄일때
            {
                if (m_strDesc.IndexOf("+", iLineChg) != -1)
                {
                    string strTemp = m_strDesc.Substring(m_strDesc.IndexOf("+", iLineChg), m_strDesc.Length - m_strDesc.IndexOf("+", iLineChg));

                    TempDesc.GetComponent<UILabel>().text = m_strDesc.Substring(iLineChg, m_strDesc.IndexOf("+", iLineChg) - iLineChg - 1);
                    TempDesc.transform.FindChild("Plus").GetComponent<UILabel>().text = strTemp;
                }
                    //TempDesc.GetComponent<UILabel>().text = m_strDesc.Substring(iLineChg, m_strDesc.IndexOf("+", iLineChg) - iLineChg - 1);
                else if (m_strDesc.IndexOf("-", iLineChg) != -1)
                {
                    string strTemp = m_strDesc.Substring(m_strDesc.IndexOf("-", iLineChg), m_strDesc.Length - m_strDesc.IndexOf("-", iLineChg));

                    TempDesc.GetComponent<UILabel>().text = m_strDesc.Substring(iLineChg, m_strDesc.IndexOf("-", iLineChg) - iLineChg - 1);
                    TempDesc.transform.FindChild("Minus").GetComponent<UILabel>().text = strTemp;
                }
                    //TempDesc.GetComponent<UILabel>().text = m_strDesc.Substring(iLineChg, m_strDesc.IndexOf("-", iLineChg) - iLineChg - 1);
                else
                    TempDesc.GetComponent<UILabel>().text = m_strDesc.Substring(iLineChg, m_strDesc.Length - iLineChg);
            }

            iLineChg = iNextLineChg;
        }


        //int tempLineCounter = 0;

        //if (m_strDesc.IndexOf("+") != -1)
        //{
        //    for (int i = 0; i < m_strDesc.IndexOf("+"); ++i)
        //    {
        //        if (m_strDesc[i] == '\n')
        //            tempLineCounter += 1;
        //    }
        //    transform.GetChild(2 + tempLineCounter).FindChild("Plus").GetComponent<UILabel>().text += m_strDesc.Substring(m_strDesc.IndexOf("+"), m_strDesc.IndexOf(" ", m_strDesc.IndexOf("+")) - m_strDesc.IndexOf("+"));
        //    transform.GetChild(2 + tempLineCounter).FindChild("Plus").GetComponent<UIAnchor>().enabled = true;
        //}

        //tempLineCounter = 0;
        //if (m_strDesc.IndexOf("-") != -1)
        //{
        //    for (int i = 0; i < m_strDesc.IndexOf("-"); ++i)
        //    {
        //        if (m_strDesc[i] == '\n')
        //            tempLineCounter += 1;
        //    }
        //    transform.GetChild(2 + tempLineCounter).FindChild("Minus").GetComponent<UILabel>().text += m_strDesc.Substring(m_strDesc.IndexOf("-"), m_strDesc.IndexOf(" ", m_strDesc.IndexOf("-")) - m_strDesc.IndexOf("-"));
        //    transform.GetChild(2 + tempLineCounter).FindChild("Minus").GetComponent<UIAnchor>().enabled = true;
        //}

        transform.FindChild("InformName").GetComponent<UILabel>().text = m_strName;
        BoxPositioning();
	}
	
	// Update is called once per frame
	void Update () {
        BoxPositioning();
	}

    void BoxPositioning()
    {
        if (Screen.width - Input.mousePosition.x < BoxSize.x)
            transform.position = UICamera.mainCamera.ScreenToWorldPoint(new Vector3(Screen.width - BoxSize.x, Input.mousePosition.y));
        else
            transform.position = UICamera.mainCamera.ScreenToWorldPoint(Input.mousePosition);
    }
}
